package com.thti.mecool.client.command.result.plan;

import java.util.List;
import com.threeti.mecool.mobile.interfaces.dto.ProjectStoreDto;
import com.thti.mecool.client.activity.MakePlanSelectProjectActivity;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.ProjectServerBizImpl;
import com.thti.mecool.client.model.po.ProjectServer;
import com.thti.mecool.client.protocol.plan.Job;
import com.thti.mecool.client.protocol.plan.Projects;
import com.thti.mecool.client.protocol.plan.TaskResult;
import com.thti.mecool.client.service.Protocol;

public class TaskCommand implements ICommand{

	@Override
	public void doCommand(Object o) {
		TaskResult taskResult=(TaskResult)o;
		if(taskResult.getStatus()==1){
			ProjectServerBizImpl projectServerBizImpl1=new ProjectServerBizImpl(Cache.getmContext());
			projectServerBizImpl1.delete();
			List<Projects> list=taskResult.getProjects();
			if(list.size()>0){
				for (int i = 0; i < list.size(); i++) {
					ProjectServer projectServer=new ProjectServer();
					projectServer.setProjectId(list.get(i).getProjectCode());
					projectServer.setName(list.get(i).getProjectName());
					projectServer.setIsType(0);
					projectServerBizImpl1.save(projectServer);
					List<ProjectStoreDto> projStores=list.get(i).getStores();
					List<Job> listTask=list.get(i).getJobs();
					if(projStores.size()>0){
						for (int j = 0; j < projStores.size(); j++) {
							ProjectServer project=new ProjectServer();
							project.setStoreId(projStores.get(j).getCode());
							project.setName(projStores.get(j).getName());
							project.setIsType(2);
							project.setProjectId(list.get(i).getProjectCode());
							projectServerBizImpl1.save(project);
						}
					}
					if(listTask.size()>0){
						for (int k = 0; k < listTask.size(); k++) {
							ProjectServer project=new ProjectServer();
							project.setTaskId(listTask.get(k).getJobId());
							project.setName(listTask.get(k).getJobName());
							project.setIsType(1);
							project.setProjectId(list.get(i).getProjectCode());
							if(listTask.get(k).isCriticalJob()){
								project.setCriticalJob(1);
							}else{
								project.setCriticalJob(0);
							}
							project.setFinishedCount(listTask.get(k).getFinishedCount());
							project.setRequiredCount(listTask.get(k).getRequiredCount());
							projectServerBizImpl1.save(project);
						}
					}
				}
			}
			//发送成功信息
			MakePlanSelectProjectActivity.handler.sendEmptyMessage(1);
		}else{
		BaseActivity.setCurrentMesg(taskResult.getMsg());
		MakePlanSelectProjectActivity.handler.sendEmptyMessage(2);
		}
		
	}

	@Override
	public String getCommandID() {
		// TODO Auto-generated method stub
		return Protocol.getTask;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return TaskResult.class;
	}

}
